package com.modules.service.sys.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.vo.Result;
import com.modules.dao.sys.DeptDao;
import com.modules.dto.sys.resp.DeptEntityResp;
import com.modules.entity.sys.DeptEntity;
import com.modules.service.sys.DeptService;

import cn.hutool.core.util.StrUtil;

/**
 * service 部门
 * @author yyq
 */
@Service
public class DeptServiceImpl implements DeptService{


    @Autowired
    private DeptDao dao;

    /**
     * 查询单个
     * @return
     */
    public Result<?> selectOne(DeptEntity dept) {
    	DeptEntity one = dao.selectOne(new LambdaQueryWrapper<DeptEntity>());
    	return Result.success(one, "获取成功");
    }
    
    /**
     * 查询
     * @return
     */

    public Result<?> select(DeptEntity dept) {
    	List<Map<String,Object>> list = selectTree(dept.getParentId(), dept.getOid());
        return Result.success(list, "获取成功");
    }

    public List<Map<String, Object>> selectTree(Integer fid, Integer oid) {
        List<Map<String, Object>> lv = new ArrayList<Map<String, Object>>();
        DeptEntity dept = new DeptEntity();
        dept.setParentId(fid);
        dept.setOid(oid);
        List<DeptEntityResp> list = dao.selectTree(dept);
        if(list != null && list.size() > 0) {
        	for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                DeptEntityResp p = list.get(i);
                map.put("did", p.getDid());
                map.put("title", p.getDname());
                map.put("oname", p.getOname());
                map.put("name", p.getDname());
                map.put("fid", p.getParentId());
                map.put("spread", true);
                map.put("oid", p.getOid());
                map.put("children", selectTree(p.getDid(), oid));
                lv.add(map);
            }
        }
        return lv;
    }
    
    /**
     * 保存
     *
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> save(DeptEntity dept) {
    	if (StrUtil.isEmpty(dept.getDname())) {
            return Result.fail("参数错误");
        }
        DeptEntity data = dao.existsData(dept);
        if (data != null) {
            if (data.getDname().equals(dept.getDname())) {
                return Result.fail("部门重复");
            }
        }
        if (dept.getDid() == null) {
            dao.insert(dept);
        } else {
            dao.updateById(dept);
        }
        return Result.success("保存成功");
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(Integer id) {
    	Integer exists = dao.exists(id);
        if (exists > 0) {
            return Result.fail("删除失败，请先删除下级");
        }
        dao.deleteById(id);
        return Result.success("删除成功");
    }

    public Result<?> selectBranch(DeptEntity dept) {
    	List<DeptEntity> list = dao.selectBranch(dept);
        return Result.success(list);
    }

    public Result<?> selectTop(DeptEntity dept) {
    	List<DeptEntity> list = dao.selectTop(dept);
        return Result.success(list);
    }
}
